package com.happy.study.servier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.study.dao.FileMapper;
import com.happy.study.entity.FileEntity;
import com.happy.study.servier.IFileService;
import com.happy.study.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Slf4j
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileEntity> implements IFileService {

    @Override
    public FileEntity querySingle ( FileEntity fileEntity ) {
        QueryWrapper<FileEntity> queryWrapper = new QueryWrapper<> ();
        if ( fileEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( fileEntity.getRelationId () != null ) {
            queryWrapper.eq ( "relationId", fileEntity.getRelationId () );
        }
        if ( fileEntity.getType () != null ) {
            queryWrapper.eq ( "type", fileEntity.getType () );
        }
        fileEntity = baseMapper.selectOne ( queryWrapper );
        return fileEntity;
    }

    /**
     * 上传图片, 指定url和上传地址
     *
     * @param fileEntity
     * @return
     */
    @Override
    public FileEntity addSingle ( FileEntity fileEntity, MultipartFile multipartFile ) {
        if ( fileEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        // 查询该userId以及type的资源是否存在
        fileEntity.setType ( ConstUtil.FILE_TYPE_IMAGE );
        FileEntity file = baseMapper.selectOne ( new QueryWrapper<FileEntity> ().eq ( "userId", fileEntity.getUserId () ).eq ( "type", fileEntity.getType () ) );
        // 指定上传的url
        String fileName = FileUtil.fileNameTreated ( multipartFile.getOriginalFilename () );
        StringBuffer url = new StringBuffer ();
        url.append ( ConstUtil.NET_DOMAIN + "/" + CommonUtil.getFormatDateTime ( ConstUtil.DATE_FORMAT ) + "/" + fileName );
        // 指定上传的硬盘地址
        String uploadDir = ConstUtil.DISK_DIRECT + CommonUtil.getFormatDateTime ( ConstUtil.DATE_FORMAT ); //D:upload/2021-02-08
        try {
            FileUtil.upload ( multipartFile, uploadDir, fileName );
        } catch ( Exception e ) {
            log.error ( "文件上传异常-> {}", e.getMessage () );
        }
        fileEntity.setFileUrl ( url.toString () );
        fileEntity.setFileDir ( uploadDir + "/" + fileName );
        fileEntity.setCreateTime ( new Date () );
        if ( file == null ) {
            int insert = baseMapper.insert ( fileEntity ); // 不存在则插入新资源
            if ( insert == 0 ) {
                throw new StateException ( StateEnum.INSERT_EXCE );
            }
        } else {
            file.setFileDir ( fileEntity.getFileDir () );
            file.setFileUrl ( fileEntity.getFileUrl () );
            file.setChangeTime ( new Date () );
            int update = baseMapper.updateById ( file ); // 存在则更新资源
            if ( update == 0 ) {
                throw new StateException ( StateEnum.UPDATE_EXCE );
            }
        }
        return fileEntity;
    }

}
